﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using Zero.DataLayer;
using Zero.DataLayer.Repository;
using Zero.POCO;
using Zero.Presenter;

namespace DevExpress.MailClient.Win.Forms.Campagne
{
    public partial class frmEditAnnonceur : XtraForm, IEditAnnonceurView
    {
        bool _isAnnonceurModified;
        bool _newAnnonceur = true;
        public Annonceur annonceur { get; set; }
        public EditionAnnonceurPresenter _editionAnnonceurPresenter;
        public Guid AnnonceurId { get; set; }


        public frmEditAnnonceur(Annonceur annonceurHist, bool newAnnonceur)
        {
            InitializeComponent();
            
            

            DialogResult = DialogResult.Cancel;
            CBEDommaine.SelectedIndex = 0;
            _newAnnonceur = newAnnonceur;

            AnnonceurId = _newAnnonceur ? default(Guid) : annonceurHist.id;




            _editionAnnonceurPresenter = new EditionAnnonceurPresenter(this, new AnnonceurRepository(), AnnonceurId);
            _editionAnnonceurPresenter.Display();


            if (!_newAnnonceur)
            {
                TEName.Text = annonceur.Name;
                CBEDommaine.SelectedItem = annonceur.Domaine;
            }

            _isAnnonceurModified = _newAnnonceur;


            GCMarque.DataSource = annonceur.Marques;
            GVMarque.Columns[2].Visible = false;
            TEName.Focus();
        }


        private void BBIAdmarqued_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            var form = new frmEditMarque(null, true,null,true);
            form.Location = new Point(Left + (Width - form.Width) / 2, Top + (Height - form.Height) / 2);
            form.ShowDialog();
            form.Activate();
            var resultMarque = form.marque;
            if (resultMarque != null)
                annonceur.Marques.Add(resultMarque);

            GCMarque.DataSource = annonceur.Marques;
            GVMarque.PopulateColumns();
            GVMarque.Columns[2].Visible = false;
            Cursor.Current = Cursors.Default;
        }


        DialogResult QueryDelete()
        {
            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.DeleteQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    DeleteRecord();
                    return DialogResult.Yes;
            }
        }

        private void DeleteRecord()
        {
            

        }


        DialogResult QueryClose()
        {
            if (!_isAnnonceurModified)
                return DialogResult.Yes;

            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.SaveQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    ApplyChanges();
                    return DialogResult.Yes;
            }
        }

        private void ApplyChanges()
        {
            

        }


        private void textEdit1_Properties_Validating(object sender, CancelEventArgs e)
        {
            if (TEName.Text.Trim().Length <= 0)
                e.Cancel = true;
        }

        private void frmEditAnnonceur_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = QueryClose();
            e.Cancel = result == DialogResult.Cancel;
        }

        private void TEName_TextChanged(object sender, EventArgs e)
        {

        }

        private void CBEDommaine_SelectedValueChanged(object sender, EventArgs e)
        {

        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            TEName.Focus(); 
            TEName.DoValidate();
            if (String.IsNullOrEmpty(TEName.Text)) return;
            //this.Validate();

            
             _isAnnonceurModified = false;
            DialogResult result = QueryClose();
            annonceur.Name = TEName.Text;
            annonceur.Domaine = CBEDommaine.SelectedText.ToDomaine();
            _editionAnnonceurPresenter.WriteChanges();
            if (result == DialogResult.Yes) this.Close();
        }

        private void barButtonadd_ItemClick(object sender, ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            var form = new frmEditMarque(null, true,null,true);
            //form.Load += OnEditMailFormLoad;
            //form.FormClosed += OnEditMailFormClosed;
            form.Location = new Point(Left + (Width - form.Width) / 2, Top + (Height - form.Height) / 2);
            form.ShowDialog();
            form.Activate();
            var resultMarque = form.marque;
            if (resultMarque != null)
                annonceur.Marques.Add(resultMarque);

            GCMarque.DataSource = annonceur.Marques;
            GVMarque.PopulateColumns();
            GVMarque.Columns[2].Visible = false;
            Cursor.Current = Cursors.Default;
        }

        private void barButtonModify_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void barButtonDelete_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void TEName_Validated(object sender, EventArgs e)
        {
        
        }

        private void TEName_Validating(object sender, CancelEventArgs e)
        {
            
        }

        private void frmEditAnnonceur_Load(object sender, EventArgs e)
        {
            this.Focus();
            TEName.Focus();
        }


        
    }
}